<template>
  <!--  <el-dialog v-model="drawer" :close-on-click-modal="false" destroy-on-close width="55%" @close="drawerClose" title="工作联系函">-->
  <!-- <el-form :model="formData" :rules="formRules" ref="formRef" size="small" :inline="false" label-width="100px" >
    <el-form-item label="发函">{{ formData.sendEnterpriseName }}</el-form-item>
    <el-form-item label="发送人">{{ formData.sendUserName }}</el-form-item>
    <el-form-item label="收函">
      <module-relate :relate-items="formData.relateItems" :selected="presetRelate"/>
    </el-form-item>
    <el-form-item label="时间" prop="actionStartTime">
      <div style="max-width: 400px">
        <el-date-picker
            v-if="dateType"
            v-model="dateTime"
            style="width: 100%"
            type="datetimerange"
            range-separator="至"
            start-placeholder="行动开始时间"
            end-placeholder="行动结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="selectTime"
        />
        <el-date-picker
            v-else
            v-model="formData.actionStartTime"
            type="datetime"
            style="width: 100%"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择行动时间"
        />
      </div>
    </el-form-item>
    <el-form-item label="主题" prop="title">
      <el-input v-model="formData.title" clearable maxlength="32" placeholder="请填写主题"/>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input type="textarea" v-model="formData.content"/>
    </el-form-item>
    <el-form-item label="上传">
      <Document style="margin-left: 30px" :file-group="formData.type" :result-list="attach" :limit="9"/>
    </el-form-item>
  </el-form> -->
  <work-contact-letter-form ref="form" :code="code" :supplierId="supplierId"></work-contact-letter-form>
  <el-row justify="end">
    <el-button size="small" @click="closeMatter">关闭</el-button>
    <el-button size="small" type="primary" @click="submitData">提交</el-button>
  </el-row>
  <!--    <template #footer>-->
  <!--      <el-button size="small" @click="closeMatter">关闭</el-button>-->
  <!--      <el-button size="small" type="primary" @click="submitData">提交</el-button>-->
  <!--    </template>-->
  <!--  </el-dialog>-->
</template>

<script>
// 工作联系函 WORK_CONTACT_LETTER
// import Cookies from 'js-cookie'
// import {formatDate} from '/@/utils/formatTime'
import {computed, reactive, ref, toRefs} from 'vue'
// import {ElMessage} from 'element-plus'
// import {getSupplierSelector} from '/@/api/supplier'
// import Document from '/@/components/document/index.vue'
// import ModuleRelate from '/@/views/matter/components/ModuleRelate.vue'
// import {createContactLetter} from '/@/api/matter'
import WorkContactLetterForm from '/@/views/matter/components/WorkContactLetterForm.vue'

export default {
  name: 'WorkContactLetter',
  components: {
    // ModuleRelate,
    // Document,
    WorkContactLetterForm
  },
  emits: ['callback', 'update:code'],
  props: {
    code: {
      type: String,
      default: ''
    },
    supplierId: {
      type: String,
      default: null
    }
  },
  setup(props, context) {
    const form = ref()
    // const {code, supplierId} = toRefs(props)
    // let drawer = computed({
    //   get: () => {
    //     let flag = code.value == 'WORK_CONTACT_LETTER'
        // if (flag) {
        //   resetForm()
        //   if (supplierId.value) {
        //     state.formData.supplierInfoId = supplierId.value
        //     state.presetRelate = {
        //       code: 'Supplier',
        //       dataId: supplierId.value
        //     }
        //   }
        //   state.formData.sendEnterpriseName = Cookies.get('enterpriseName')
        //   state.formData.sendUserName = Cookies.get('userName')
        //   state.formData.executorIds.push(Cookies.get('userId'))
        //   // 行动时间默认现在
        //   state.formData.actionStartTime = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS')
        //   // 获取供应商列表
        //   getSupplierSelector().then(res => state.supplierList = res.data).catch(e => console.log(e))
        // }
        // return flag
      // },
      // set: val => context.emit('update:code', val ? 'WORK_CONTACT_LETTER' : '')
    // })

    // const state = reactive({
    //   formData: {
    //     type: 'WORK_CONTACT_LETTER'
    //   },
    //   formRules: {
    //     title: {required: true, message: '请填写主题', trigger: 'blur'}
    //   },
    //   supplierList: [],
    //   dateType: false,  // 行动时间范围/单体切换
    //   dateTime: [],
    //   attach: [],
    //   presetRelate: null  // 预设默认选中关联项
    // })

    const submitData = () => {
      form.value.submit()
      // formEl.validate(valid => {
      //   if (!valid) {
      //     return false
      //   }
      //   let params = JSON.parse(JSON.stringify(state.formData))
      //   // 列表转逗号分割字符串提交
      //   if (state.attach.length > 0) {  // 组装附件地址和名称
      //     for (let i = 0; i < state.attach.length; i++) {
      //       let item = state.attach[i]
      //       params.attach += item.src
      //       params.attachNames += item.name
      //       if (i < state.attach.length - 1) {
      //         params.attach += ','
      //         params.attachNames += '，'
      //       }
      //     }
      //   }

      //   // TODO 提交工作函事项
      //   createContactLetter(params).then(res => {
      //     if (res.data) {
      //       ElMessage.success('提交成功！')
      //       closeMatter()
      //     }
      //   }).catch(e => console.log(e))
      // })
    }
    // const selectTime = (val) => {
    //   if (val && val.length === 2) {
    //     state.formData.actionStartTime = val[0]
    //     state.formData.actionEndTime = val[1]
    //   } else {
    //     state.formData.actionStartTime = state.formData.actionEndTime = null
    //   }
    // }
    const resetForm = () => {
      form.value.reset()
      // state.formData = {
      //   id: null,
      //   title: '工作联系函',
      //   content: '',
      //   sendUserName: null,
      //   sendEnterpriseName: null,
      //   supplierInfoId: null,
      //   actionStartTime: null,
      //   actionEndTime: null,
      //   executorIds: [],
      //   relateItems: [],
      //   remark: null,
      //   attach: '',
      //   attachNames: '',
      //   priority: 0,
      //   remindState: 'NONE',
      //   remindType: '',
      //   type: 'WORK_CONTACT_LETTER'
      // }
    }
    const closeMatter = () => {
      context.emit('update:code', '')
    }
    const drawerClose = () => {
      // 重置
      resetForm()
      context.emit('callback')
    }

    return {
      // drawer,
      form,
      // ...toRefs(state),
      drawerClose,
      closeMatter,
      resetForm,
      // selectTime,
      submitData
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
